const {
  $util,
} = getApp();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    apps: [],
    isShowLoading: true, // 是否显示loading
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
    swiperCurrent: 0,
    current: 0,
    isNone: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(ops) {
    this.setData(ops);
  },
  onShow() {
    this.getList();
  },
  getList() {
    $util.request({
      url: 'entry/wxapp/base.def.apps',
      data: {
        type: 'work', // 'dynamic','study','work','mine'
        version: 1,
      },
      showLoading: this.data.isShowLoading,
      success: (res) => {
        console.log(res)
        this.getBannerList();
        this.setData({
          swiperCurrent: 0,
          current: 0,
          apps: res.apps,
          list: res.items,
          navnum: res.navnum,
          navset: res.navset,
          isShowLoading: false,
          isNone: res.items.length > 0 ? false : true,
        });
      },
    });
  },
  /**
  * 获取轮播图
  */
  getBannerList() {
    $util.request({
      url: 'entry/wxapp/system.slide.index',
      data: {
        type: 2,
      },
      showLoading: false,
      success: (res) => {
        this.setData({
          bannerList: res.data,
          isNone: res.data.length > 0 ? false : true,
        });
      },
    });
  },
  navigateTo(e) {
    let { url, title = '', isresult = '', hasvote = '' } = e.currentTarget.dataset;
    let urlArr = url.split('?');
    let paramArr = {};
    title && (paramArr.barTitle = title);
    if (urlArr.length > 1) {
      let param = urlArr[1].split('&');
      for (let i = 0; i < param.length; i++) {
        paramArr[param[i].split('=')[0]] = param[i].split('=')[1];
      }
    }
    if (isresult == '' && hasvote == '') {
      app.util.navigateTo(urlArr[0], paramArr);
    } else {
      if (isresult == 1 || hasvote == 0) {
        app.util.navigateTo(urlArr[0], paramArr);
      } else {
        wx.showToast({
          title: '该投票结果不可查看',
          icon: 'none',
          duration: 1500,
        });
      }
    }
  },
  bannerJump(e) {
    let {
      ztype,
      path = '',
      appid = '',
      title,
    } = e.currentTarget.dataset.item;
    if (Number(ztype) === 1) {
      let urlArr = path.split('?');
      let paramArr = {};
      paramArr.barTitle = title;
      if (urlArr.length > 1) {
        let param = urlArr[1].split('&');
        for (let i = 0; i < param.length; i++) {
          paramArr[param[i].split('=')[0]] = param[i].split('=')[1];
        }
      }
      app.util.navigateTo(urlArr[0], paramArr);
    } else if (Number(ztype) === 2) {
      wx.navigateToMiniProgram({
        appId: appid,
        path,
        envVersion: 'release',
      });
    } else if (Number(ztype) === 3) {
      const url = `/ws_party_const/pages/web/web?webview_url=${path}`;
      wx.navigateTo({
        url,
      });
    }
  },
  checkAuth(e) {
    let { url } = e.currentTarget.dataset;
    $util.checkauth(url, () => { this.navigateTo(e) });
  },
  swiperChange(e) {
    this.setData({
      swiperCurrent: e.detail.current,
    });
  },
  changeCurrentSwiper(e) {
    this.setData({
      swiperCurrent: e.currentTarget.dataset.idx,
    });
  },

  onShareAppMessage: function() {

  },
  onPullDownRefresh() {
    wx.stopPullDownRefresh();
    this.getList();
  },
});